package com.ruoyi.web.controller.system;


import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.nlp.protocol.INlpService;
import com.ruoyi.system.domain.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自然语言处理
 *
 * @author ruoyi
 * @date 2022-08-27
 */
@RestController
@RequestMapping("/system/nlp")
public class SysNlpController {

    @Lazy
    @Autowired
    private INlpService nlpService;

    /**
     * 词性标注
     *
     * @throws Exception
     */
    @RequestMapping("posTaggingByWords")
    public R posTaggingByWords(@RequestBody Map<String,Object> params) throws Exception {
        String text = params.get("text")+"";
        HashMap<String, Object> r = new HashMap<String, Object>();
        List<HashMap<String, Object>> posTaggingList = nlpService.posTagging(text);
        List<HashMap<String, String>> posCategoryTempList = nlpService.getAllPosCategory();
        // 返回前台标注分类变量
        List<HashMap<String, String>> posCategoryList = new ArrayList<HashMap<String, String>>();

        // 循环类别list，如标注里面包括此code则放入另一个list容器
        for (int i = 0; i < posCategoryTempList.size(); i++) {
            HashMap<String, String> posCategory = posCategoryTempList.get(i);
            for (int j = 0; j < posTaggingList.size(); j++) {
                HashMap<String, Object> posTagging = posTaggingList.get(j);
                if (posTagging.get("nature").toString()
                        .equals(posCategory.get("code").toString())) {
                    posCategoryList.add(posCategory);
                    break;
                }
            }
        }
        // 标注结果
        r.put("posTaggingList", posTaggingList);
        // 标注来源
        r.put("posCategoryList", posCategoryList);
        return R.success().dataAll(r);
    }

    /**
     * 自动摘要
     * @throws Exception
     */
    @RequestMapping("automaticSummary")
    public R automaticSummary(@RequestBody Map<String,Object> params) throws Exception {
        String text = params.get("text")+"";
        HashMap<String, Object> r = new HashMap<String, Object>();
        List<String> summaryText = nlpService.autoSummary(text, 5);
        // 标注结果
        r.put("summaryText", summaryText);
        return R.success().dataAll(r);
    }

    /**
     * 关键词提取
     *
     * @throws Exception
     */
    @RequestMapping("keywordExtraction")
    public R keywordExtraction(@RequestBody Map<String,Object> params) throws Exception {
        String text = params.get("text")+"";
        HashMap<String, Object> r = new HashMap<String, Object>();
        List<String> keywordList = nlpService.autoKeyword(text, 5);
        r.put("keywordList", keywordList);
        return R.success().dataAll(r);
    }




}
